/**
 * 
 */
package com.jie.eclipse.pan.editors.form;

import java.util.HashMap;
import java.util.Map;

import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.StackLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.forms.IDetailsPage;
import org.eclipse.ui.forms.IFormPart;
import org.eclipse.ui.forms.IManagedForm;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.forms.widgets.Section;
import org.eclipse.ui.forms.widgets.TableWrapData;

import com.jie.eclipse.pan.data.BaseDataEntity;
import com.jie.eclipse.pan.swtitem.SWTHelper;
import com.jie.eclipse.pan.swtitem.widgets.BaseComposite;
import com.jie.eclipse.pan.utils.handle.FileHandle;
import com.jie.eclipse.pan.xml.XMLNode;

/**
 * @author yanjie
 * 
 */
public class PllDetailPage implements IDetailsPage {
	private BasePage formPage = null;
	private IManagedForm mform;
	private XMLNode node;//当前选中的节点
	private Section detailSection = null;//明细部分
	private Composite client = null ;
	

	private BaseComposite curComposite = null;//当前部件
	private Map<String,BaseComposite> cpsMap = null;
	
	private BaseDataEntity tempRecord = null;
	private SWTHelper swtHelper = null;
	private FileHandle fileHandle = null; 
	
	/**
	 * 
	 */
	public PllDetailPage(BasePage formPage) {
		this.formPage = formPage;
		this.cpsMap = new HashMap<String, BaseComposite>();
		this.swtHelper = new SWTHelper(this);
		this.fileHandle = new FileHandle(this);
	}
	
	@SuppressWarnings("deprecation")
	public void createContents(Composite parent) {
		// 设置父类面板的布局
		GridLayout layout= new GridLayout();  
		layout.marginLeft = 5;
		layout.marginRight = 2;
		layout.marginBottom = 2;
		parent.setLayout(layout);
		
		// 获得表单工具对象
		FormToolkit toolkit = mform.getToolkit();
		// 创建Detail的内容区
		detailSection = toolkit.createSection(parent,Section.TITLE_BAR);
		detailSection.setLayoutData(new GridData(GridData.FILL_BOTH));
		detailSection.setText("详细内容");
			
		// 创建内容区的所设置的面板
		client = toolkit.createComposite(detailSection,SWT.SCROLL_PAGE);
		detailSection.setClient(client);
		StackLayout stackLayout = new StackLayout();
		client.setLayout(stackLayout);
	}

	public void initialize(IManagedForm form) {
		this.mform = form;
	}

	public void dispose() {

	}

	public boolean isDirty() {
		return false;
	}

	public void commit(boolean onSave) {

	}

	public boolean setFormInput(Object input) {

		return false;
	}

	public void setFocus() {

	}

	public boolean isStale() {
		return false;
	}

	public void refresh() {

	}

	// 当Master区域选中事件发生时
	public void selectionChanged(IFormPart part, ISelection selection) {
		// 首先获得选中的对象
		IStructuredSelection currentSelection = (IStructuredSelection) selection;
		if (currentSelection.size() == 1)
			node = (XMLNode) currentSelection.getFirstElement();
		// 如果选中的对象不为null,则刷新控件所显示的值
		
		if (node != null){
			update();
		}
	}
	
	@SuppressWarnings("deprecation")
	public void update() {
		if(this.cpsMap.containsKey(node.getNodeName()) && !(
				"table".equalsIgnoreCase(node.getNodeName())
				|| "tablecolumn".equalsIgnoreCase(node.getNodeName())
				)){
			this.curComposite = this.cpsMap.get(node.getNodeName());
		}else{
			this.curComposite = swtHelper.getBaseComposite(node);
			this.cpsMap.put(node.getNodeName(), this.curComposite);
		}
		
		if(tempRecord == null) tempRecord = new BaseDataEntity();//初始化临时记录
		node.FillBaseDataEntity(tempRecord, true, false);//将当前的node的属性填充到临时记录中
		this.curComposite.getDataBind().setRecord(this.tempRecord);
		
		//刷新布局
		((StackLayout)this.client.getLayout()).topControl=this.curComposite.getComposite();
		this.client.layout();
	}
	
	
	public IManagedForm getMform() {
		return mform;
	}

	public Section getDetailSection() {
		return detailSection;
	}
	
	public XMLNode getCurNode() {
		return node;
	}

	public BaseComposite getCurComposite() {
		return curComposite;
	}
	
	public FileHandle getFileHandle() {
		return fileHandle;
	}
	
	public BasePage getFormPage() {
		return formPage;
	}
	
	public Map<String, BaseComposite> getCpsMap() {
		return cpsMap;
	}
	
	public PllPage getPllpage(){
		return (PllPage)this.formPage;
	}
	
	public Composite getClient() {
		return client;
	}
}
